package com.zx._12_算法.剑指offer._2022;

import java.util.HashMap;

public class 第一个只出现一次的字符 {

    public char firstUniqChar1(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for (char c : chars) {
            if (map.get(c) == 1) {
                return c;
            }
        }
        return ' ';
    }

    public char firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        int[] res = new int[26];
        for (char c : chars) {
            res[c - 'a'] = res[c - 'a'] + 1;
        }
        for (char c : chars) {
            if (res[c - 'a'] == 1) {
                return c;
            }
        }
        return ' ';
    }
}
